package com.imooc.course.system.controller.admin;

import com.alibaba.fastjson.JSON;
import com.imooc.course.system.form.ResourceForm;
import com.imooc.course.system.form.PageForm;
import com.imooc.course.server.dto.ResourceDto;
import com.imooc.course.server.dto.PageDto;
import com.imooc.course.server.dto.ResponseDto;
import com.imooc.course.server.service.ResourceService;
import com.imooc.course.server.utils.CopyUtil;
import com.imooc.course.server.utils.ValidatorUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/admin/resource")
public class ResourceController {

    private static final Logger log = LoggerFactory.getLogger(ResourceController.class);
    public static final String BUSINESS_NAME = "资源";


    @Resource(name = "resourceService")
    private ResourceService resourceService;

    /**
     * 展示资源树
     * @return
     */
    @GetMapping("/list")
    public ResponseDto getList() {
//        List<ResourceDto> list = resourceService.loadTree();
        List<ResourceDto> list = resourceService.getList();
        return ResponseDto.success().setContent(list);
    }

    /**
     * 保存资源
     * @param request
     * @return
     */
    @PostMapping("/save")
    public ResponseDto addResource(HttpServletRequest  request) {
        String resourceStr = request.getParameter("resourceStr");
        //入参检查
        ValidatorUtil.notNull(resourceStr, "资源描述文本");

        //JSON.parseArray()方法可以深度转换一个数据，深度也就是数组中嵌套多个数组，每个数组中还有嵌套。
        List<ResourceDto> resourceDtoList = JSON.parseArray(resourceStr, ResourceDto.class);

        resourceService.save(resourceDtoList);

        return ResponseDto.success();
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public ResponseDto del(@PathVariable String id) {
        resourceService.delete(id);
        return ResponseDto.success();
    }
}
